<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
      <el-form-item label="参数类型" prop="scope">
        <el-select
          v-model="queryParams.scope"
          placeholder="请选择"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in scopeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>
    <el-row>
      <el-col>
        <el-table v-loading="loading" :data="roleList">
          <el-table-column label="Id" width="45px" prop="id"  v-if="columns[0].visible"/>
          <el-table-column label="参数类型" width="90px" prop="scope" :formatter="scopeFormat" v-if="columns[1].visible"/>
          <el-table-column label="参数名称" prop="configName"  v-if="columns[2].visible"/>
          <el-table-column label="参数值" prop="value" v-if="columns[3].visible"/>
          <el-table-column label="单位" prop="unit"  v-if="columns[4].visible"/>
          <el-table-column label="描述" prop="description"  v-if="columns[5].visible"/>
          <el-table-column
            label="操作"
            align="center"
            width="160"
            class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                @click="handleUpdate(scope.row)"
              >编辑</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="参数名称" prop="configName">
          <el-input v-model="form.configName" placeholder="请输入参数名称" />
        </el-form-item>
        <el-form-item label="参数值" prop="value">
          <el-input v-model="form.value" placeholder="请输入参数值" />
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入单位" />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入参数描述" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { configList, updateConfig} from "@/api/biz"

export default {
  name: "BtcConfig",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 角色表格数据
      roleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        scope: ''
      },
      scopeOptions:[
        {
          dictValue:'common',
          dictLabel:'通用'
        },
        {
          dictValue:'cru',
          dictLabel:'CRU矿池'
        },
        {
          dictValue:'xch',
          dictLabel:'XCH矿池'
        },
        {
          dictValue:'btc',
          dictLabel:'BTC矿池'
        },
        {
          dictValue:'bzz',
          dictLabel:'BZZ矿池'
        },
        {
          dictValue:'pha',
          dictLabel:'PHA矿池'
        }
      ],
      // 表单参数
      form: {
        id:'',
        configName:'',
        value:'',
        unit:'',
        description:''
      },
      // 列信息
      columns: [
        { key: 0, label: `Id`, visible: true },
        { key: 1, label: `参数类型`, visible: true },
        { key: 2, label: `参数名称`, visible: true },
        { key: 3, label: `参数值`, visible: true },
        { key: 4, label: `单位`, visible: true },
        { key: 5, label: `描述`, visible: true }
      ],
      // 表单校验
      rules: {
        configName: [
          { required: true, message: "参数名称不能为空", trigger: "blur" }
        ],
        value: [
          { required: true, message: "参数值不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 状态字典翻译
    scopeFormat(row, column) {
      return this.selectDictLabel(this.scopeOptions, row.scope);
    },
    /** 查询系统参数列表 */
    getList() {
      this.loading = true;
      configList(this.addDateRange(this.queryParams, this.dateRange)).then(
        response => {
          this.roleList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    // 表单重置
    reset() {
      this.form = {
        id:'',
        configName:'',
        value:'',
        unit:'',
        description: ''
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form.id = row.id
      this.form.configName = row.configName
      this.form.value = row.value
      this.form.unit = row.unit?row.unit:''
      this.form.description = row.description? row.description: ''
      this.open = true;
      this.title = "修改参数";
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateConfig(this.form).then(response => {
            this.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
        }
      });
    }
  }
};
</script>
